<template>
  <div class="customerService">
    <div class="btn_box" v-show="!service" @click="openChatFrame"></div>
    <aside>
      <Modal
        v-model="service"
        :mask-closable="false"
        :closable="false"
        width="760"
        :style="modalStyle">
        <div class="frame_box">
          <div class="head">
            <div class="head_image">
              <img :src="headImage.serviceImage" alt="">
            </div>
            <span>客服1号</span>
          </div>
          <div class="body">
            <div class="a">
              <div class="b">
                <div class="chart_box">
                  <ul>
                    <li
                      v-for="(item,index) in chatList"
                      :key="index">
                      <div class="a_s a_box" v-if="item.type===1">
                        <p>{{item.time}}</p>
                        <div class="b_b">
                          <div class="b_box b_head">
                            <div class="headImage"><img :src="headImage.serviceImage" alt=""></div>
                          </div>
                          <div class="b_box b_content">
                            <div class="chatBox">
                              <p>{{item.text}}</p>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="a_u a_box" v-if="item.type===2">
                        <p v-if="chatList[index].timeStamp - chatList[index-1].timeStamp > 60000 || index===0">{{item.time}}</p>
                        <div class="b_b">
                          <div class="b_box b_content">
                            <div class="chatBox">
                              <p>{{item.text}}</p>
                            </div>
                          </div>
                          <div class="b_box b_head">
                            <div class="headImage"><img :src="headImage.userImage" alt=""></div>
                          </div>
                        </div>
                      </div>
                    </li>
                  </ul>
                </div>
              </div>
              <div class="b">
                <div class="handle_box">
                  <ul>
                    <li v-for="(item,index) in handleList" :key="index">
                      <span class="iconfont" :class="item.icon"></span>
                    </li>
                  </ul>
                </div>
                <div class="input_box">
                  <textarea v-model="sendText" @keyup="send"></textarea>
                </div>
                <div class="btn_box">
                  <button @click="sendChatText(sendText)">发送</button>
                </div>
              </div>
            </div>
            <div class="a">
              <img src="../../assets/imgs/service_right.png" alt="">
            </div>
          </div>
        </div>
        <div slot="footer">
          <button @click="closeChatFrame">
            <span class="iconfont icon-arrow-down"></span>
          </button>
        </div>
      </Modal>
    </aside>
  </div>
</template>

<script>
  import headImage from '../../apiData/service/headImage.js'
  export default {
    name: "customerService",  // 在线客服
    data() {
      return {
        headImage: {},
        service: false,
        modalStyle: {},
        chatList: [],  // 聊天内容
        handleList: [
          { value: 0, label: '表情', icon: 'icon-biaoqing' },
          { value: 1, label: '图片', icon: 'icon-tupian' }
        ],
        sendText: ''
      }
    },
    created() {
      this.getChartList();
      this.getData()
    },
    methods: {
      getData() {
        this.headImage = headImage
      },
      // 获取聊天记录
      getChartList() {
        let chatRecords = JSON.parse(localStorage.getItem('chatRecords'));
        if(chatRecords && chatRecords !== 'null' && chatRecords.length) {
          this.chatList = chatRecords
        }
      },
      // 添加聊天信息
      addChat(chatText, type) {
        let timestamp = Date.now();
        let date = this.getDate();
        let tempText = {
          type: type,  // 聊天对象  null：非客服非用户  1：客服  2：用户
          timeStamp: timestamp,  // 时间戳
          time: date,  // 格式化的时间
          text: chatText  // 聊天内容
        };
        this.chatList.push(tempText);
        localStorage.setItem('chatRecords',JSON.stringify(this.chatList));
      },
      // 打开聊天框
      openChatFrame() {
        this.service = true;
        let text = '您好，欢迎您了解 LET管理系统，有什么需要帮您的呢……如果在线不方便沟通，您可以留下电话/微信，我们会以最快的速度联系您的！';
        if(this.chatList && this.chatList.length>0) {
          //  表示有聊天记录，暂不做操作
          // console.log(this.chatList);
        }else{
          this.addChat(text,1)
        }
      },
      getDate() {
        let cd = new Date();
        let date = this.formatDate(cd);
        return date
      },
      formatDate(cd) {
        let M = cd.getMonth()+1;  // 月
        let D = cd.getDate();  // 日
        let h = cd.getHours();  // 小时
        let m = cd.getMinutes();  // 分钟
        return M + '月' + D + '日 ' + ' ' + h + ':' + m
      },
      send(e) {
        if(e.keyCode === 13) {
          this.sendChatText(this.sendText)
        }
      },
      // 发送
      sendChatText(text) {
        if(text) {
          this.addChat(text, 2);
        }else{
          this.$Message.warning('输入框不能为空！')
        }
        this.sendText = '';  // 清空输入框
      },
      // 关闭聊天框
      closeChatFrame() {
        this.service = false
      }
    }
  }
</script>

<style scoped lang="scss">
  @import "../../style/scrollbar.css";  // 滚动条样式
  .customerService {
    position: fixed;
    bottom: 20px;
    right: 30px;
    width: 142px;
    height: 50px;
    z-index: 200;
    .btn_box{
      width: 100%;
      height: 100%;
      background-image: url(../../assets/imgs/service.png);
      background-repeat: no-repeat;
      -webkit-background-size: contain;
      background-size: contain;
      cursor: pointer;
      opacity: 0.8;
      transition: all 0.5s;
    }
    .btn_box:hover{
      opacity: 1;
    }
  }
</style>
<style lang="scss" scoped>
  /deep/ .ivu-modal{
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 1px 1px 15px 0 #555;
    .ivu-modal-body{
      padding: 0;
      .frame_box{
        .head{
          padding: 10px 20px;
          background: linear-gradient(270deg,#1890ff,#3875ea);
          color: #fff;
          display: flex;
          align-items: center;
          .head_image{
            width: 30px;
            height: 30px;
            padding: 3px;
            background: #fff;
            border-radius: 50%;
            margin-right: 12px;
            img{
              width: 24px;
              height: 24px;
              border-radius: 50%;
            }
          }
          span{
            font-size: 15px;
            letter-spacing: 1px;
          }
        }
        .body{
          height: 600px;
          display: flex;
          .a:first-child{
            width: 500px;
            border-right: 1px solid #ddd;
            .b:first-child{
              height: calc(100% - 200px);
              border-bottom: 1px solid #ddd;
            }
            .b:last-child{
              height: 200px;
            }
            .b{
              .chart_box::-webkit-scrollbar {
                width: 1px; /*对垂直流动条有效*/
                height: 1px; /*对水平流动条有效*/
              }
              .chart_box{
                width: 100%;
                height: 100%;
                overflow-y: auto;
                ul{
                  li{
                    margin: 8px 0;
                    .a_box{
                      font-size: 12px;
                      p{
                        text-align: center;
                        color: #515a6e;
                      }
                      .b_b{
                        display: flex;
                        .b_head{
                          width: 40px;
                          height: 30px;
                          .headImage{
                            margin: 3px auto;
                            width: 24px;
                            height: 24px;
                            padding: 2px;
                            border-radius: 50%;
                            img{
                              width: 20px;
                              height: 20px;
                              border-radius: 50%;
                            }
                          }
                        }
                        .b_content{
                          max-width: calc(100% - 40px - 160px);
                          padding: 6px 10px;
                          margin: 0 10px;
                          position: relative;
                          background: #f5f5f5;
                          border-radius: 6px;
                          .chatBox{
                            p{
                              text-align: left;
                            }
                          }
                        }
                        .b_content::before{
                          content: "";
                          display: inline-block;
                          width: 0;
                          height: 0;
                          position: absolute;
                        }
                      }
                    }
                    .a_s{
                      .b_b{
                        .b_content::before{
                          border-top: 7px solid transparent;
                          border-bottom: 7px solid transparent;
                          border-right: 12px solid #f5f5f5;
                          top: 8px;
                          left: -12px;
                        }
                      }
                    }
                    .a_u{
                      .b_b{
                        justify-content: right;
                        .b_content{
                          background: #9fea6d;
                        }
                        .b_content::before{
                          border-top: 7px solid transparent;
                          border-bottom: 7px solid transparent;
                          border-left: 12px solid #9fea6d;
                          top: 8px;
                          right: -12px;
                        }
                      }
                    }
                  }
                }
              }
              .handle_box{
                height: 40px;
                display: flex;
                align-items: center;
                ul{
                  display: flex;
                  align-items: center;
                  li{
                    margin: 0 8px;
                    span{
                      color: #000;
                      font-size: 19px;
                      cursor: pointer;
                    }
                  }
                }
              }
              .input_box{
                height: calc(100% - 40px - 50px);
                textarea{
                  width: 100%;
                  height: 100%;
                  resize:none;
                  padding: 10px;
                  outline: none;
                  border: none;
                }
              }
              .btn_box{
                position: relative;
                height: 50px;
                text-align: right;
                button{
                  border: none;
                  border-radius: 3px;
                  background: rgba(56, 117, 234, 0.9);
                  color: #fff;
                  font-size: 12px;
                  padding: 3px 10px;
                  letter-spacing: 1px;
                  position: absolute;
                  top: 50%;
                  right: 30px;
                  transform: translate(0, -50%);
                  cursor: pointer;
                }
                button:hover{
                  background: rgba(56, 117, 234, 1);
                }
              }
            }
          }
          .a:last-child{
            width: calc(100% - 500px);
            display: flex;
            align-items: center;
            img{
              width: 100%;
            }
          }
        }
      }
    }
    .ivu-modal-footer{
      border-top: none;
      padding: 0;
      button{
        background: transparent;
        border: none;
        position: absolute;
        top: 8px;
        right: 16px;
        cursor: pointer;
        span{
          font-size: 24px;
          color: #fff;
        }
      }
      button:hover{
        span{
          opacity: 0.7;
        }
      }
    }
  }
</style>
